package ship.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ship.entity.usertable;
import ship.service.shipservice;

/**
 * Servlet implementation class registbyuserServlet
 */
@WebServlet("/registbyuserServlet")
public class registbyuserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public registbyuserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		int id = Integer.parseInt( request.getParameter("sid"));
		String name = request.getParameter("sname");
	    usertable user = new usertable(id,name);
	    shipservice service = new shipservice();
	    boolean result = service.registusertable(user);
	    if(result) {
	    	  request.setAttribute("sid", id);
	    	  request.setAttribute("sname", name);
	    	  request.getRequestDispatcher("loginbyuserServlet").forward(request, response);
	    }else { 
              String mess = "该编号已存在或该姓名已存在";
              request.setAttribute("mess", mess); 
              request.getRequestDispatcher("registbyuser.jsp").forward(request, response);			    		
	  }
    }
}
